package weaver.interfaces.cyjt.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.cyjt.duss.utils.ArchiveUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-06-05
 * @Description: 文件传阅单调用预归档接口到预归档库
 * @ModifyLog:
 **/
public class WeaWjcydlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String requestname = requestInfo.getRequestManager().getRequestname();
        writeLog(">>>>>WeaWjcydlcAction>>>>>requestid=" + requestid);
        try {
            String message = "";
            RecordSet rs = new RecordSet();
            ArchiveUtil archiveUtil = new ArchiveUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            //文件
            String wj = Util.null2String(rs.getString("wj"));
            String wjbd = Util.null2String(rs.getString("wjbd"));
            String docids = (!wj.isEmpty() && !wjbd.isEmpty())?wj+","+wjbd:wj+wjbd;
            //文号
            String wh = Util.null2String(rs.getString("wh")," ");
            //保管期限
            String bgqx = Util.null2String(rs.getString("bgqx")," ");
            //成文日期
            String cwrq = Util.null2String(rs.getString("cwrq")," ");
            //页数
            String ys = Util.null2String(rs.getString("ys")," ");
            //密级
            String mj = Util.null2String(rs.getString("mj")," ");
            //解密划控
            String jmhk = Util.null2String(rs.getString("jmhk")," ");
            //所属人员
            String ssry = Util.null2String(rs.getString("ssry"), " ");
            //所属部门
            String ssbm = Util.null2String(rs.getString("ssbm"), " ");

            JSONObject paramsJson = new JSONObject();
            JSONArray datasList = new JSONArray();
            JSONObject datasJson = new JSONObject();
            JSONObject arcJson = new JSONObject();
            arcJson.put("appId", "xbivbhbzz2");
            JSONArray filesList = new JSONArray();
            JSONObject filesJson = new JSONObject();
            JSONArray documentList = new JSONArray();
            if(!docids.isEmpty()){
                String[] docidsArr = docids.split(",");
                for (int i = 0; i < docidsArr.length; i++) {
                    JSONObject documentJson = new JSONObject();
                    documentJson.put("needDownLoad", "true");
                    documentJson.put("ftpName", "");
                    JSONArray paramList = new JSONArray();
                    Map<String, Object> imagefileInfo = archiveUtil.getImagefileInfo(docidsArr[i]);
                    JSONObject paramJson1 = new JSONObject();
                    JSONObject paramJson2 = new JSONObject();
                    String imagefileid = Util.null2String(imagefileInfo.get("imagefileid"));
                    String ddcode = archiveUtil.getTokenKey(imagefileid);
                    String baseUrl = Util.null2String(getPropValue("archive", "oaurl"));
                    String fileDownload = baseUrl+"/weaver/weaver.file.FileDownload?fileid=" + imagefileid + "&download=1&ddcode=" + ddcode;
                    paramJson1.put("name", "path");
                    paramJson1.put("title", "附件地址");
                    paramJson1.put("content", fileDownload);
                    paramJson2.put("name", "arc_document_name");
                    paramJson2.put("title", "附件名称");
                    paramJson2.put("content", Util.null2String(imagefileInfo.get("imagefilename")));
                    paramList.add(paramJson1);
                    paramList.add(paramJson2);
                    documentJson.put("param", paramList);
                    documentList.add(documentJson);
                }
            }

            filesJson.put("document", documentList);
            JSONObject metadataJson = new JSONObject();
            metadataJson.put("type", "3");
            JSONArray fieldList = new JSONArray();
            JSONObject owner_deptJson = new JSONObject();
            owner_deptJson.put("name", "owner_dept");
            owner_deptJson.put("title", "所属部门");
            owner_deptJson.put("content", archiveUtil.getDeptId(ssbm));
            fieldList.add(owner_deptJson);
            JSONObject bgqxJson = new JSONObject();
            bgqxJson.put("name", "bgqx");
            bgqxJson.put("title", "保管期限");
            bgqxJson.put("content", bgqx);
            fieldList.add(bgqxJson);
            JSONObject ssfbJson = new JSONObject();
            ssfbJson.put("name", "owner_company");
            ssfbJson.put("title", "所属分部");
            ssfbJson.put("content", "1");
            fieldList.add(ssfbJson);
            JSONObject dzywidJson = new JSONObject();
            dzywidJson.put("name", "owner_id");
            dzywidJson.put("title", "电子原文id");
            dzywidJson.put("content", wj);
            fieldList.add(dzywidJson);
            JSONObject kfqkJson = new JSONObject();
            kfqkJson.put("name", "kfqk");
            kfqkJson.put("title", "开放情况");
            kfqkJson.put("content", jmhk);
            fieldList.add(kfqkJson);
            JSONObject ssryJson = new JSONObject();
            ssryJson.put("name", "owner_user");
            ssryJson.put("title", "所属人员");
            ssryJson.put("content", archiveUtil.getUserId(ssry));
            fieldList.add(ssryJson);
            JSONObject lssjidJson = new JSONObject();
            lssjidJson.put("name", "form_biz_id_old");
            lssjidJson.put("title", "历史数据id");
            lssjidJson.put("content", "1");
            fieldList.add(lssjidJson);
            JSONObject whJson = new JSONObject();
            whJson.put("name", "wh");
            whJson.put("title", "文号");
            whJson.put("content", wh);
            fieldList.add(whJson);
            JSONObject btJson = new JSONObject();
            btJson.put("name", "bt");
            btJson.put("title", "标题");
            btJson.put("content", requestname);
            fieldList.add(btJson);
            JSONObject fjscJson = new JSONObject();
            fjscJson.put("name", "arc_document");
            fjscJson.put("title", "附件上传");
            fjscJson.put("content", wj);
            fieldList.add(fjscJson);
            JSONObject sjlxJson = new JSONObject();
            sjlxJson.put("name", "data_type");
            sjlxJson.put("title", "数据类型");
            sjlxJson.put("content", "0");
            fieldList.add(sjlxJson);
            JSONObject gldjJson = new JSONObject();
            gldjJson.put("name", "relationNums");
            gldjJson.put("title", "关联单据");
            gldjJson.put("content", "1");
            fieldList.add(gldjJson);
            JSONObject ysJson = new JSONObject();
            ysJson.put("name", "ys");
            ysJson.put("title", "页数");
            ysJson.put("content", ys);
            fieldList.add(ysJson);
            JSONObject cwrqJson = new JSONObject();
            cwrqJson.put("name", "cwrq");
            cwrqJson.put("title", "成文日期");
            cwrqJson.put("content", cwrq);
            fieldList.add(cwrqJson);
            JSONObject mjJson = new JSONObject();
            mjJson.put("name", "mj");
            mjJson.put("title", "密级");
            mjJson.put("content", mj);
            fieldList.add(mjJson);
            metadataJson.put("field", fieldList);
            filesJson.put("metadata", metadataJson);
            filesList.add(filesJson);
            datasJson.put("arc", arcJson);
            datasJson.put("files", filesList);
            datasList.add(datasJson);
            paramsJson.put("datas", datasList);
            JSONObject resultJson = archiveUtil.sendPostOKHttpRequest(paramsJson.toJSONString());
            writeLog(">>>>WeaWjcydlcAction>>>>resultJson=" + resultJson);
            message = Util.null2String(resultJson.getString("message"));
            if ("success".equals(message)) {
                JSONObject returnData = resultJson.getJSONObject("data");
                JSONArray returnResultList = returnData.getJSONArray("result");
                String status = returnResultList.getJSONObject(0).getString("status");
                if ("true".equals(status)) {
                    return Action.SUCCESS;
                } else {
                    message = returnResultList.getJSONObject(0).getString("messsage");

                }
            }
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("档案管理接口：" + message);
            return Action.FAILURE_AND_CONTINUE;

        } catch (Exception e) {
            writeLog(">>>>>WeaZjjhtbsplcCreatejhidAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }


}